# __author__ = 'ChenHongyan'
import pymysql
import os
from common.read_data import rd
from common.logger import logger
import pymysql.cursors

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
data_file_path = os.path.join(BASE_PATH, "config", "setting.ini")
data = rd.load_ini(data_file_path)["mysql_order"]
data1 = rd.load_ini(data_file_path)["tis_order"]
data2=rd.load_ini(data_file_path)['shopxo_order']

DB_CONF = {
    "host": data["MYSQL_HOST"],
    "port": int(data["MYSQL_PORT"]),
    "user": data["MYSQL_USER"],
    "password": data["MYSQL_PASSWD"],
    "db": data["MYSQL_DB"]
}
DB_CONF1 = {
    "host": data1["TIS_HOST"],
    "port": int(data1["TIS_PORT"]),
    "user": data1["TIS_USER"],
    "password": data1["TIS_PASSWD"],
    "db": data1["TIS_DB"]
}
DB_CONF2 = {
    "host": data2["SHOPXO_HOST"],
    "port": int(data2["SHOPXO_PORT"]),
    "user": data2["SHOPXO_USER"],
    "password": data2["SHOPXO_PASSWD"],
    "db": data2["SHOPXO_DB"]
}

class MysqlDb():

     def __init__(self,**DB_CONF):
         # 通过字典拆包传递配置信息，建立数据库连接
         self.conn = pymysql.connect(**DB_CONF, autocommit=True)
         # 通过 cursor() 创建游标对象，并让查询结果以字典格式输出
         self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

     def __del__(self):  # 对象资源被释放时触发，在对象即将被删除时的最后操作
         # 关闭游标
         self.cur.close()
         # 关闭数据库连接
         self.conn.close()

     def select_db(self, sql):
         """查询"""
         # 检查连接是否断开，如果断开就进行重连
         self.conn.ping(reconnect=True)
         # 使用 execute() 执行sql
         self.cur.execute(sql)
         # 使用 fetchall() 获取查询结果
         data = self.cur.fetchall()
         return data

db = MysqlDb(**DB_CONF)
